﻿using System;
using System.Windows;
using System.Json;
using Microsoft.Phone.Controls;
using System.Collections.Generic;

namespace FlickrWP7
{
    public partial class AuthLocation : PhoneApplicationPage
    {
        public AuthLocation()
        {
            InitializeComponent();
        }

        private void browserNavigate_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AuthBrowser.xaml",UriKind.Relative));
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string miniToken = string.Format("{0}-{1}-{2}", txtTokenA.Text, txtTokenB.Text, txtTokenC.Text);

            Dictionary<string,string> param = new Dictionary<string,string>();
            param.Add("mini_token",miniToken);
            param.Add("format", "json");
            param.Add("api_key", App.API_KEY);
            param.Add("method", "flickr.auth.getFullToken");


            Core.Authentication.GetSignature(param, App.SECRET, () =>
                {
                    Core.Authentication.GetFullToken(miniToken, Core.Authentication.Signature, App.API_KEY, () =>
                        {
                            string temp = string.Empty;

                            try
                            {
                                JsonObject json = JsonObject.Parse(Core.Json.MakeValidJson(Core.Authentication.FullToken)) as JsonObject;
                                JsonValue value = null;
                                json.TryGetValue("auth", out value);
                                json = JsonObject.Parse(value.ToString()) as JsonObject;
                                json.TryGetValue("token", out value);
                                json = JsonObject.Parse(value.ToString()) as JsonObject;
                                json.TryGetValue("_content", out value);
                                temp = value.ToString().Remove(0, 1);
                                temp = temp.Remove(temp.Length - 1, 1);
                                System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["token"] = temp;
                                System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Save();

                                json = JsonObject.Parse(Core.Json.MakeValidJson(Core.Authentication.FullToken)) as JsonObject;
                                json.TryGetValue("auth", out value);
                                json = JsonObject.Parse(value.ToString()) as JsonObject;
                                json.TryGetValue("user", out value);
                                json = JsonObject.Parse(value.ToString()) as JsonObject;
                                json.TryGetValue("username", out value);
                                temp = value.ToString().Remove(0, 1);
                                temp = temp.Remove(temp.Length - 1, 1);

                                try
                                {
                                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["user"] = temp;
                                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Save();
                                }
                                catch
                                {
                                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Add("user", temp);
                                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Save();
                                }

                                json.TryGetValue("nsid", out value);
                                temp = value.ToString().Remove(0, 1);
                                temp = temp.Remove(temp.Length - 1, 1);

                                try
                                {
                                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings["id"] = temp;
                                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Save();
                                }
                                catch
                                {
                                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Add("id", temp);
                                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Save();
                                }

                                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
                            }
                            catch
                            {
                                MessageBox.Show("Cannot authorize the user with the specified token. Please try again later or use a different token.");
                            }
                        });
                });

        }
    }
}